package com.example.demo.exception;

import javafx.scene.control.Alert;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;
import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.InvocationTargetException;

/**
 * @Author hweiyu
 * @Description
 * @Date 2022/6/28 17:34
 */
@Setter
@Accessors(chain = true)
@Slf4j
public class ExceptionHandler {

    private Throwable e;

    public void handle() {
        if (null == e) {
            return;
        }
        log.error("异常信息：", e);
        String msg;
        //获取真正抛出的异常类
        Throwable t = e;
        while (null != t.getCause()) {
            t = t.getCause();
        }
        if (t instanceof BusinessException) {
            BusinessException be = (BusinessException) t;
            msg = be.getMsg();
        } else {
            msg = t.getMessage();
        }
        if (null == msg || "".equals(msg)) {
            msg = ErrorCode.UNKNOWN.getMsg();
        }
        Alert alert = new Alert(Alert.AlertType.ERROR);
        alert.setContentText(msg);
        alert.setHeaderText(null);
        alert.setTitle("错误");
        alert.showAndWait();
    }
}
